home *** CD-ROM | disk | FTP | other *** search
- Path: news.ruhr-uni-bochum.de!usenet
- From: Karsten Soete <Karsten.Soete@rz.ruhr-uni-bochum.de>
- Newsgroups: comp.lang.c++
- Subject: Re: Return
- Date: 20 Apr 1996 14:43:50 GMT
- Organization: Ruhr-Universitaet Bochum, Rechenzentrum
- Message-ID: <4lat76$hh3@sun168.rz.ruhr-uni-bochum.de>
- NNTP-Posting-Host: dialslip-39.rz.ruhr-uni-bochum.de
-
- morrisd@nevada.edu (DAMON MORRIS) schreibt:
- > I am having a really bad problem with some return statements...
- > I declare a function to return an int value then in the function
- > I check to see if two strings are equal and if so then I use another
- > variable to see what to return...
- >
- > int charisma(int ability, char *choice)
- > {
- > if (choice == "Loyalty Base")
- > {
- > if (ability==10) return 20;
- > }
- > }
- >
- > This little program will never work for some reason. When I call it
- > like this charisma(10,"Loyalty Base") it always returns some number
- > that it shouldn't. The number that is returned is usually between
- > 5000-10000. If I wrote the value of choice to the screen before it
- > checked to see if it was equal to "Loyalty Base" it always says that
- > it is equal to "Loyalty Base", yet it never gets to the second if.
- > I have no idea what is the matter...I will send anyone full source if
- > needed to fix the problem, but the source is 1000+ lines....
- >
- >
-
- First the equal-operator (==) must be implented for char *.
- This is the case only for string-classes and I think it doesn't
- apply here. To compare to strings conventionally use strcmp.
-
- I your case a simple pointer-comparation between choice and the
- memory-location of "Loayalty Base" is done. This won't work.
-
- So the comparation never matches and the return-value in the
- if-statement can't be returned.
- By the way, what then. Your function doesn't define a return-value
- in case the comparation won't match.
-
- Try this code:
- #include <string.h>
-
- int charisma(int ability, char *choice){
- if(!strcmp(choice,"Loyalty Base"))
- if(ability==10) return 20;
-
- return 10; //in case comparations don't match.
- }
-